package ProjectMain;


public class Event
    {
        private String summary, location, comments, resources, frequency = "";
        private String[] days = new String[7];
        private iCalDate start = new iCalDate();
        private iCalDate end = new iCalDate();
        private iCalDate recEnd = new iCalDate();
        String months[] = {"--","January", "February", "March", "April", "May",
                        "June", "July", "August", "September", "October", "November", "December"};
        //For non recurring events
        public Event(String sum, String loc, String c, String r, iCalDate s, iCalDate e)
        {
        	summary = sum;
            location = loc;
            comments = c;
            resources = r;
            start = s;
            end = e;
        }
        //For recurring events
        public Event(String sum, String loc, String c, String r, iCalDate s, iCalDate e,
                        iCalDate rec, String f)
        {
        	summary = sum;
            location = loc;
            comments = c;
            resources = r;
            start = s;
            end = e;
            frequency = f;
            recEnd = rec;
        }
        //For classes
        public Event(String sum, String loc, String c, iCalDate s, iCalDate e,
        		iCalDate rec, String f, String[] d)
        {
        	summary = sum;
        	location = loc;
        	comments = c;
        	start = s;
        	end = e;
        	frequency = f;
        	recEnd = rec;
        	days = d;
        }
        public boolean isRecurring()
        {
        	if(this.frequency.equalsIgnoreCase("None") || this.frequency.isEmpty())
            {
        		return false;
            }
            else
            {
            	return true;
            }
        }
        public boolean isTBA()
        {
        	if(days[0].equalsIgnoreCase("TBA"))
        	{
        		return true;
        	}
        	return false;
        }
        public String evtIcs()
        {
        	String ics = "";
            if(!this.isRecurring())
            {
            	ics =   "CLASS:" + "PUBLIC" + "\n" +
                        "SUMMARY:" + summary + "\n" +
                        "COMMENT:" + comments + "\n" +
                        "LOCATION:" + location + "\n" +
                        "RESOURCES:" + resources + "\n" +
                        "DTSTART;TZID=Pacific/Honolulu:" + start.getYear() + start.getMonth() + start.getDay() + "T" + start.getHour() + start.getMin() + "00" + "Z" + "\n" +
                        "DTEND;TZID=Pacific/Honolulu:" + end.getYear() + end.getMonth() + end.getDay() + "T" + end.getHour() + end.getMin() + "00" + "Z" + "\n";
                return ics;
            }
            else
            {
            	String recurr = " ";
                if(this.frequency.compareToIgnoreCase("YEARLY")==0)
                {
                	recurr = "FREQ=YEARLY;UNTIL=" + recEnd.getYear() + recEnd.getMonth() + recEnd.getDay();;
                    ics =
                    "CLASS:" + "PUBLIC" + "\n" +
                    "SUMMARY:" + summary + "\n" +
                    "COMMENT:" + comments + "\n" +
                    "LOCATION:" + location + "\n" +
                    "RESOURCES:" + resources + "\n" +
                    "DTSTART;TZID=Pacific/Honolulu:" + start.getYear() + start.getMonth() + start.getDay() + "T" + start.getHour() + start.getMin() + "00" + "Z" + "\n" +
                    "DTEND;TZID=Pacific/Honolulu:" + end.getYear() + end.getMonth() + end.getDay() + "T" + end.getHour() + end.getMin() + "00" + "Z" + "\n" +
                    "RRULE:" + recurr + "\n";
                    return ics;
                }
                else if(this.frequency.compareToIgnoreCase("MONTHLY")==0)
                {
                	recurr = "FREQ=MONTHLY;UNTIL=" + recEnd.getYear() + recEnd.getMonth() + recEnd.getDay();
                    ics =   "CLASS:" + "PUBLIC" + "\n" +
                            "SUMMARY:" + summary + "\n" +
                            "COMMENT:" + comments + "\n" +
                            "LOCATION:" + location + "\n" +
                            "RESOURCES:" + resources + "\n" +
                            "DTSTART;TZID=Pacific/Honolulu:" + start.getYear() + start.getMonth() + start.getDay() + "T" + start.getHour() + start.getMin() + "00" + "Z" + "\n" +
                            "DTEND;TZID=Pacific/Honolulu:" + end.getYear() + end.getMonth() + end.getDay() + "T" + end.getHour() + end.getMin() + "00" + "Z" + "\n" +
                            "RRULE:" + recurr + "\n";
                    return ics;
                }
                else if(this.frequency.compareToIgnoreCase("WEEKLY") == 0)
                {
                	recurr = "FREQ=WEEKLY;INTERVAL=1;UNTIL=" + recEnd.getYear() + recEnd.getMonth() + recEnd.getDay()
                			+ ";WKST=SU;BYDAY=";
                	StringBuilder sb = new StringBuilder();
                	for(int i = 0; i < days.length; i++)
                	{
                		if(i == days.length-1)
                		{
                			if(days[i] != null)
                			{
                        		sb.append(days[i]);
                			}
                		}
                		else
                		{
                			if(days[i] != null)
                			{
                    			sb.append(days[i]);
                    			sb.append(",");
                			}
                		}
                	}
                	sb.append("\n");
                	recurr = recurr + sb.toString();
                    ics =   "CLASS:" + "PUBLIC" + "\n" +
                            "SUMMARY:" + summary + "\n" +
                            "COMMENT:" + comments + "\n" +
                            "LOCATION:" + location + "\n" +
                            "RESOURCES:" + resources + "\n" +
                            "DTSTART;TZID=Pacific/Honolulu:" + start.getYear() + start.getMonth() + start.getDay() + "T" + start.getTime() + "00" + "Z" + "\n" +
                            "DTEND;TZID=Pacific/Honolulu:" + end.getYear() + end.getMonth() + end.getDay() + "T" + end.getTime() + "00" + "Z" + "\n" +
                            "RRULE:" + recurr;
                    return ics;
                }
                else if(this.frequency.compareToIgnoreCase("DAILY")==0)
                {
                	recurr = "FREQ=DAILY;UNTIL=" + recEnd.getYear() + recEnd.getMonth() + recEnd.getDay();
                    ics =   "CLASS:" + "PUBLIC" + "\n" +
                            "SUMMARY:" + summary + "\n" +
                            "COMMENT:" + comments + "\n" +
                            "LOCATION:" + location + "\n" +
                            "RESOURCES:" + resources + "\n" +
                            "DTSTART;TZID=Pacific/Honolulu:" + start.getYear() + start.getMonth() + start.getDay() + "T" + start.getHour() + start.getMin() + "00" + "Z" + "\n" +
                            "DTEND;TZID=Pacific/Honolulu:" + end.getYear() + end.getMonth() + end.getDay() + "T" + end.getHour() + end.getMin() + "00" + "Z" + "\n" +
                            "RRULE:" + recurr + "\n";
                    return ics;
                }
                else
                {
                	return ics;
                }
            }
        }
        public String toString()
        {
        	String result = " ";
            if(this.isRecurring())
            {
            	if(this.frequency.compareToIgnoreCase("WEEKLY") == 0)
            	{
            		result = summary + " at " + location + " [" + comments + "] on " + months[Integer.parseInt(start.getMonth())] + " " + start.getDay() + ", " + start.getYear()
                    		+ " at " + start.getTime() + " recurring " + this.frequency +
                            " until: " + months[Integer.parseInt(recEnd.getMonth())] + " " + recEnd.getDay() + ", " + recEnd.getYear();
            	}
            	else
            	{
            		result = summary + " at " + location + " [" + comments + "] on " + months[Integer.parseInt(start.getMonth())] + " " + start.getDay() + ", " + start.getYear()
                    		+ " at " + start.getHour() + " recurring " + this.frequency +
                            " until: " + months[Integer.parseInt(recEnd.getMonth())] + " " + recEnd.getDay() + ", " + recEnd.getYear();
            	}
                return result;
            }
            else
            {
            	result = summary + " at " + location + " [" + comments + "] on " + months[Integer.parseInt(start.getMonth())] + " " + start.getDay() + ", " + start.getYear()
                        + " at " + start.getHour() + start.getMin() + " to " + 
                        months[Integer.parseInt(end.getMonth())] + " " + end.getDay() + ", " + end.getYear()
                        + " at " + end.getHour() + end.getMin();
                return result;
            }
        }
    }